

function HandleInput ( )

    if GetVirtualButtonPressed ( 1 )
        if GetJoystickX ( ) = 0
            MoveSaucer ( -1, BurstUp )
        endif
        if GetJoystickX ( ) < -0.5
            MoveSaucer ( -1, BurstLeft )
        endif
        if GetJoystickX ( ) > 0.5
            MoveSaucer ( -1, BurstRight )
        endif
    endif

endfunction

function AIInput ( saucerIndex )
    if saucerIndex > 0

        select ( eSaucer [ saucerIndex ].aiType )

            case 1:
                // Basic AI
                select ( eSaucer [ saucerIndex ].aiState )

                    case 1:
                        // Idle
                        if GetSpriteYByOffset ( eSaucer [ saucerIndex ].Sprite ) > eSaucer [ saucerIndex ].TargetY
                            if GetSpritePhysicsVelocityY ( eSaucer [ saucerIndex ].Sprite ) > -100 then MoveSaucer ( saucerIndex, BurstUp )
                        else
                            if GetSpritePhysicsVelocityY ( eSaucer [ saucerIndex ].Sprite ) > 100 then MoveSaucer ( saucerIndex, BurstUp )
                        endif
                    endcase

                    case 2:
                        // Patrol
                        select ( eSaucer [ saucerIndex ].Zone )

                            case 1:
                                // Top
                                if GetSpriteXByOffset ( eSaucer [ saucerIndex ].Sprite ) <= eSaucer [ saucerIndex ].TargetX
                                    MoveSaucer ( saucerIndex, BurstRight )
                                else
                                    eSaucer [ saucerIndex ].TargetX = eSaucer [ saucerIndex ].TargetX + 300
                                    if eSaucer [ saucerIndex ].TargetX > 800 then eSaucer [ saucerIndex ].TargetX = 300 - eSaucer [ saucerIndex ].TargetX - 800
                                endif
                            endcase

                            case 2:
                                // Middle
                                eSaucer [ saucerIndex ].aiState = 1
                            endcase

                            case 3:
                                // Bottom
                                eSaucer [ saucerIndex ].aiState = 1
                            endcase

                        endselect
                    endcase

                    case 3:
                        // Seek
                    endcase

                endselect
            endcase

        endselect

    endif
endfunction

function MoveSaucer ( saucerIndex, Direction )
    select Direction

        case BurstLeft
            if saucerIndex = -1
                // Add line : Start animation
                SetSpritePhysicsImpulse ( pSaucer.Sprite, GetSpriteXByOffset( pSaucer.Sprite ), GetSpriteYByOffset( pSaucer.Sprite ), -100, -100 )
                pSaucer.State = Moving
            else
                // Add line : Start animation
                SetSpritePhysicsImpulse ( eSaucer [ saucerIndex ].Sprite, GetSpriteXByOffset( eSaucer [ saucerIndex ].Sprite ), GetSpriteYByOffset( eSaucer [ saucerIndex ].Sprite ), -100, -100 )
                eSaucer [ saucerIndex ].State = Moving
            endif
        endcase

        case BurstUp
            if saucerIndex = -1
                // Add line : Start animation
                SetSpritePhysicsImpulse ( pSaucer.Sprite, GetSpriteXByOffset( pSaucer.Sprite ), GetSpriteYByOffset( pSaucer.Sprite ), 0, -100 )
                PlaySprite ( pSaucer.Sprite, 30 )
                pSaucer.State = Moving
            else
                // Add line : Start animation
                SetSpritePhysicsImpulse ( eSaucer [ saucerIndex ].Sprite, GetSpriteXByOffset( eSaucer [ saucerIndex ].Sprite ), GetSpriteYByOffset( eSaucer [ saucerIndex ].Sprite ), 0, -100 )
                eSaucer [ saucerIndex ].State = Moving
            endif
        endcase

        case BurstRight
            if saucerIndex = -1
                // Add line : Start animation
                SetSpritePhysicsImpulse ( pSaucer.Sprite, GetSpriteXByOffset( pSaucer.Sprite ), GetSpriteYByOffset( pSaucer.Sprite ), 100, -100 )
                pSaucer.State = Moving
                pSaucer.StateTimerGoal = timer() + 0.2
            else
                // Add line : Start animation
                SetSpritePhysicsImpulse ( eSaucer [ saucerIndex ].Sprite, GetSpriteXByOffset( eSaucer [ saucerIndex ].Sprite ), GetSpriteYByOffset( eSaucer [ saucerIndex ].Sprite ), 100, -100 )
                eSaucer [ saucerIndex ].State = Moving
            endif
        endcase

        case RollLeft

        endcase

        case RollRight

        endcase

    endselect
endfunction
